package edu.guet.server.service;

import edu.guet.server.mapper.*;
import edu.guet.server.model.*;
import edu.guet.server.model.SemesterStatistic.SemesterPermissions;
import edu.guet.server.model.SemesterStatistic.SemesterTask;
import edu.guet.server.model.dto.AddSemester;
import edu.guet.server.model.dto.ShowSemesterDTO;
import edu.guet.server.model.dto.StuUnitTest;
import edu.guet.server.model.vo.ExcellentCourseVO;
import edu.guet.server.utils.CreateSemesterCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class SemesterService {
    @Autowired
    SemesterMapper semesterMapper;
    @Autowired
    private UnitMapper unitMapper;
    @Autowired
    private UnitTestMapper unitTestMapper;
    @Autowired
    private PaperMapper paperMapper;
    @Autowired
    UHomeworkService uHomeworkService;
    @Autowired
    ExcellentCourseMapper excellentCourseMapper;
    @Autowired
    private UHomeorkMapper uHomeorkMapper;

    /**
     * 添加学期课程并返回主键
     *
     * @param addsemester
     * @return
     */

    public Integer addSemester(AddSemester addsemester) {
        Semester semester = new Semester();
        semester.setTech_id(addsemester.getTech_id());
        semester.setCourse_id(addsemester.getCourse_id());
//        semester.setSemester_intro(addsemester.getSemester_intro());
        semester.setSemester_name(addsemester.getSemester_name());
        semester.setStart_time(addsemester.getStart_time());
        semester.setEnd_time(addsemester.getEnd_time());
        //复制码
        String code = CreateSemesterCode.finalCreate();
        log.info("课程复制码{}", code);
        semester.setSemester_code(code);
        semesterMapper.addSemester(semester);
        return semester.getSemester_id();
    }

    /**
     * 修改学期课程信息
     *
     * @param semester
     */

    public void updateSemester(Semester semester) {
        semesterMapper.updateSemester(semester);
    }

    /**
     * 添加学期
     *
     * @param id
     * @return
     */
    public Semester findSemesterById(Integer id) {
        Semester semester = semesterMapper.findSemesterById(id);
        return semester;
    }

    /**
     * 通过id找到学期
     *
     * @param semIds
     * @param status 需要查询的课程状态没有就填null
     * @return
     */
    public List<ShowSemesterDTO> findSemesterByIds(List<Integer> semIds, Integer status) {
        return semesterMapper.findSemesterandReByIds(semIds, status);
    }

    /**
     * 更新课堂是否截止状态
     *
     * @param semesterId
     */
    public void updateSemStatus(Integer semesterId, Integer status) {
        semesterMapper.updateSemStatus(semesterId, status);
    }

    /**
     * 刷新加课人数
     */
    public void updateSemStuCnt(Integer semId) {
        semesterMapper.updateSemStuCnt(semId);
    }

    /**
     * 负责人姓名以及其他课程基本信息
     *
     * @param semesterId
     * @return
     */
    public SemesterTask findSemesterTask(Integer semesterId) {
        SemesterTask semesterTask = semesterMapper.findSemesterTask(semesterId);
        return semesterTask;
    }

    public SemesterPermissions findPermissionsById(Integer semesterId) {
        return semesterMapper.findPermissionsById(semesterId);
    }

    @Transactional
    public void copySem(Semester sem) {
        Semester semester = semesterMapper.findSemesterByCOde(sem.getSemester_code());

        Integer oldSemesterId = semester.getSemester_id();
        log.info("老的semID:{}", oldSemesterId);
        //复制码
        String code = CreateSemesterCode.finalCreate();
        log.info("课程复制码{}", code);
        semester.setSemester_code(code);
//        semester.setSemester_id(null);
        semester.setTech_id(sem.getTech_id());
        semester.setSemester_name(sem.getSemester_name());
        semester.setStart_time(sem.getStart_time());
        semester.setEnd_time(sem.getEnd_time());
        semester.setSemester_status(-1);

        semesterMapper.addSemester(semester);
        Integer newSemesterId = semester.getSemester_id();
        log.info("新的semID:{}", newSemesterId);

        List<Unit> oldUnits=unitMapper.findsemUnits(oldSemesterId);
        for (Unit unit : oldUnits) {
            Integer oldUnitId = unit.getUnit_id();
            unit.setSemester_id(newSemesterId);
            unitMapper.setUnit(unit);
            //resource_cnt
            Integer newUnitId = unit.getUnit_id();
            log.info("oldUnitId:{}", oldUnitId);
            log.info("newUnitId:{}", newUnitId);
            //单元测试
            List<StuUnitTest> unitTests=unitTestMapper.findTests(oldUnitId);
            for (StuUnitTest unitTest : unitTests) {
                Integer oldUnitTestId=unitTest.getUnit_id();
                log.info("oldUnitTestId:{}", oldUnitTestId);
                unitTest.setUnit_id(newUnitId);
                unitTestMapper.insertUnitTest(unitTest);
                Integer newUnitTestId=unitTest.getUnit_test_id();
                log.info("newUnitTestId:{}", newUnitTestId);
                //试卷库
                Integer PaperId=paperMapper.getPaperId(oldUnitTestId);
                paperMapper.addTestPaper(newUnitTestId,PaperId);
            }
            //作业
            List<UHomework> unitHomeworks=uHomeorkMapper.findhmk(oldUnitId);
            for (UHomework unitHomework : unitHomeworks) {
//                Integer oldUnitTestId= unitHomework.getUnit_id();
                unitHomework.setUnit_id(newUnitId);
                uHomeorkMapper.insertUnitHmk(unitHomework);
            }
        }
    }

    public List<ExcellentCourseVO> findSemesterByCourseId(Integer courseId) {
        return  excellentCourseMapper.findCourseInfo(courseId);
    }

    public void deleteSemById(Integer semesterId) {
        semesterMapper.deleteSemById(semesterId);
    }
}
